// pages/entry/entry.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pid:3,
    political:['预备团员','团员','优秀团员','预备党员','党员','优秀党员'],
    birthday:'2000-06-08',
    aid:20,
    dname:['财务部','销售部','开发部','人事部','后勤部'],
    holderjob: ['会计', '营销专员','系统分析员','人事专员','水电工'],
    did:2,sid:1999,cid:199,
    deid:['1','2','3','4','5']
  },
  SetArray(){
    var sal = [];
    for(var i = 3001;i<=12000;i++){
      sal.push(i)
    }
    this.setData({
      sal: sal
    })
    var comm = [];
    for (var i = 1; i <= 1000; i++) {
      comm.push(i)
    }
    this.setData({
      comm: comm
    })
    var age = [];
    for (var i = 1; i <= 70; i++) {
      age.push(i)
    }
    this.setData({
      age: age
    })
  },
  DeptnoChange(e){
    this.setData({
      did: e.detail.value
    })
  },
  DateChange(e){
    console.log(e)
    this.setData({
      birthday: e.detail.value
    })
    var birthday = this.data.birthday;
    var year = birthday.substring(0, 4)
    var age = 2020 - year;
    this.setData({
      aid:age - 1
    })
  },
  AgeChange(e) {
    this.setData({
      aid: e.detail.value
    })
    //1999-06-08
    var birthday = this.data.birthday;
    var year = birthday.substring(0,4)
    var day = birthday.substring(4,10)
    var age = this.data.age[this.data.aid];
    this.setData({
      birthday: (2020 - age) + day
    })
    console.log(2020 - age) + day
  },
  PolChange(e) {
    this.setData({
      pid: e.detail.value
    })
  },
  DnameChange(e){
    this.setData({
      did: e.detail.value
    })
  },
  SalChange(e){
    this.setData({
      sid: e.detail.value
    })
  },
  CommChange(e){
    this.setData({
      cid: e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.SetArray()
    var that = this;
    wx.request({
      url: 'http://192.168.1.6:8080/WeChatJSON//getMaxEmpno',
      data: {
      }, method: 'GET',
      header: {
        'content-type': 'application/json'
      },
      success:function(res){
        console.log(res)
        var empno = res.data;
        that.setData({
          empno : empno
        })
      }
    })
  },

  forsubmit:function(e){
    console.log(e)
    var empno = e.detail.value.empno;
    var ename = e.detail.value.ename;
    var health = e.detail.value.health;
    var job = e.detail.value.job;
    var birthday = this.data.birthday;
    var age = this.data.age[this.data.aid];
    var political = this.data.political[this.data.pid];
    var deptno = this.data.deid[this.data.did];
    var dname = this.data.dname[this.data.did];
    var sal = this.data.sal[this.data.sid];
    var comm = this.data.comm[this.data.cid];

    if (ename == ''){
      wx.showToast({
        title: '姓名不能为空',
        icon:'none'
      })
    } else if (health == '') {
      wx.showToast({
        title: '身体状况不能为空',
        icon: 'none'
      })
    } else if (job == '') {
      wx.showToast({
        title: '职位不能为空',
        icon: 'none'
      })
    } else{
      var that = this
      wx.request({
        url: 'http://192.168.1.6:8080/WeChatJSON//insertEmp',
        data: {
          empno: empno,
          ename: ename,
          health: health,
          job: job,
          birthday: birthday,
          age: age,
          political: political,
          deptno: deptno,
          dname: dname,
          sal: sal,
          comm: comm
        }, method: 'GET',
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          console.log(res)
          if (res.data == "入职成功"){
            wx.showToast({
              title: '入职成功',
              icon:'success'
            })
            setTimeout(function () {
              wx.navigateBack({
              })
            }, 1500) 
          }else{
            wx.showToast({
              title: res.data,
              icon:'none'
            })
          }
        }
      })
    }

  }

})